﻿using System;
using System.Security.Claims;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using SageKing.Blazor.OpenIdService.Components.Application.Pages.Manage.Components;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Icon = AntDesign.Icon;

namespace SageKing.Blazor.OpenIdService.Components.Base;

public class AuthComponentTabIndexBase<T, K, E> : AuthComponentTabBase
    where T : IEntityBaseId<K>, new()
    where E : IAddEdit<T, K>
{
    #region 参数
    /// <summary>
    /// 当前可视高，不包TopOffset
    /// </summary>
    public virtual double currPageHeight { get; set; } = 600;
    public virtual double tableHeightLeft { get; set; } = 600;
    public virtual double tableHeightRight { get; set; } = 300;
    public virtual double tableHeightRightOld { get; set; } = 300;

    /// <summary>
    /// 右边 表格高
    /// </summary>
    public virtual double tableHeightRightBottomPre { get; set; } = 0;
    #endregion

    #region init system

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            UpdateHeightAction();

            CalTableHeight();

            StateHasChanged();

        }
    }

    #endregion

    #region	表格相关

    /// <summary>
    /// 刷新各个表格的高度
    /// </summary>
    public virtual void CalTableHeight()
    {
        currPageHeight = browserHeight - TopOffset - IndexToolBarHeight;
        tableHeightLeft = currPageHeight - TableToolBarHeight - PaginationHeight;
        CalTable2Height(currPageHeight * tableHeightRightBottomPre);
    }

    public virtual void CalTable2Height(double height)
    {
        tableHeightRight = height - IndexToolBarHeight - TableToolBarHeight - PaginationHeight - SplitterWidth;

        StateHasChanged();
    }
    public override void UpdateOtherHeightAction()
    {
        CalTableHeight();
        base.UpdateOtherHeightAction();
    }

    public virtual void OnResizeHandlerMain(FluentMultiSplitterResizeEventArgs e)
    {
        Console.WriteLine($"Pane {e.PaneIndex} Resize (New size {e.NewSize})");
    }

    public virtual void OnResizeHandlerRight(FluentMultiSplitterResizeEventArgs e)
    {
        Console.WriteLine($"Pane {e.PaneIndex} Resize (New size {e.NewSize})");
        switch (e.PaneIndex)
        {
            case 1:
                //table2Height 计算
                var tmpHeight = (currPageHeight * e.NewSize / 100) + SplitterWidth;
                CalTable2Height(tmpHeight);
                tableHeightRightOld = tableHeightRight;
                break;
            default:
                break;
        }
    }

    public virtual void OnCollapseExpand(FluentMultiSplitterEventArgs e)
    {
        bool willCollapse = !e.Pane.Collapsed;
        Console.WriteLine($"Pane {e.PaneIndex} {(willCollapse ? "collapsed" : "expanded")}");

        switch (e.PaneIndex)
        {
            case 0:
                if (willCollapse)
                {
                    tableHeightRightOld = tableHeightRight;
                    //table2Height 计算
                    CalTable2Height(currPageHeight);
                }
                else
                {

                    tableHeightRight = tableHeightRightOld;
                }
                break;
            default:
                break;
        }
    }
    #endregion

    #region	Edit

    public T EditModel { get; set; } = default;
    public E EditForm { get; set; }

    public virtual bool IsCanEdit
    {
        get
        {
            return !(EditModel.IsNullOrEmpty() || EditModel.Id.IsNullOrEmpty());
        }
    }

    /// <summary>
    /// 用于显示名称
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public virtual string GetDisplayName(T model)
    {
        return model.Id?.ToString();
    }
    #endregion

    #region	Tool相关
    public virtual async Task OnToolClickAsync(ToolEventArgs e)
    {
        try
        {

            switch (e.ButtonName)
            {
                case BtnNameConsts.Add:
                    EditModel = new();
                    await this.EditForm.FocusFirst();
                    break;
                case BtnNameConsts.Edit:
                    if (!IsCanEdit)
                    {
                        _ = Message.ErrorAsync("请先选择左侧一行数据");
                    }
                    break;
                case BtnNameConsts.Save:
                    var tmpSave = await this.EditForm.Me.Save();
                    if (!tmpSave)
                    {
                        _ = Message.SuccessAsync("保存失败");
                        return;
                    }
                    EditModel = this.EditForm.model;
                    _ = Message.SuccessAsync("保存成功");
                    break;
                case BtnNameConsts.Delete:
                    if (!IsCanEdit)
                    {
                        _ = Message.ErrorAsync("请先选择左侧一行数据后再试,谢谢!");
                        return;
                    }
                    var confirmResult = await ConfirmService.Show(
                        $"删除后不可恢复，确认删除【{GetDisplayName(EditModel)}】？"
                        , "确认删除?"
                        , ConfirmButtons.YesNo
                        , ConfirmIcon.Warning,
                        new ConfirmButtonOptions { });
                    if (confirmResult == ConfirmResult.Yes)
                    {
                        var isDel = await this.EditForm.Me.Delete(EditModel.Id);

                        if (!isDel)
                        {
                            _ = Message.ErrorAsync("删除失败");
                            return;
                        }
                        EditModel = new();
                        _ = Message.SuccessAsync("删除成功");
                    }
                    else
                    {
                        _ = Message.SuccessAsync("取消删除");
                    }
                    break;
                default:
                    await OnToolDefaultClickAsync(e);
                    break;
            }

            await Task.CompletedTask;
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }
    }
    public virtual async Task OnToolDefaultClickAsync(ToolEventArgs e)
    {
        _ = Message.InfoAsync($"You clicked {e.ButtonName} button");
        await Task.Delay(100);
    }
    #region CRUD

    #endregion
    #endregion

    #region Left
    public virtual async void OnLeftRowClick(RowData<T> data)
    {
        await Task.CompletedTask;
    }
    #endregion

}
